package com.hanter.android.radui.mvp;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.hanter.android.radui.R;


public class ContentLoadStatusViewHelper {

    private final Context context;
    private final ViewGroup contentContainer; // 放置 加载视图、异常视图的容器View
    private final View contentView;
    @Nullable
    protected View loadingView;
    @Nullable
    protected View exceptionView;
    private View.OnClickListener onClickListener;

    public ContentLoadStatusViewHelper(Fragment context, ViewGroup contentContainer, View contentView) {
        this.context = context.getContext();
        this.contentContainer = contentContainer;
        this.contentView = contentView;

        if (context instanceof View.OnClickListener) {
            onClickListener = (View.OnClickListener) context;
        }
    }

    public ContentLoadStatusViewHelper(Activity context, ViewGroup contentContainer, View contentView) {
        this.context = context;
        this.contentContainer = contentContainer;
        this.contentView = contentView;

        if (context instanceof View.OnClickListener) {
            onClickListener = (View.OnClickListener) context;
        }
    }

    protected void initViews(boolean showLoading) {
        if (showLoading) {
            initLoadingView();
        }
    }

    private void initLoadingView() {
        this.loadingView = createLoadingView(contentContainer);
        this.contentContainer.addView(loadingView);
    }

    protected View createLoadingView(ViewGroup parent) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.view_content_loading,
                parent, false);
    }

    public void showLoadingView() {
        hideContentView();

        hideExceptionView();

        if (loadingView == null) {
            initLoadingView();
        }
        loadingView.setVisibility(View.VISIBLE);
    }

    // 策略：显示contentView时，移除loadingView。而显示statusView时，隐藏loadingView.

    public void removeLoadingView() {
        if (loadingView != null) {
            contentContainer.removeView(loadingView);
            loadingView = null;
        }
    }

    public void hideLoadingView() {
        if (loadingView != null) {
            loadingView.setVisibility(View.GONE);
        }
    }

    private void initExceptionView() {
        if (exceptionView == null) {
            exceptionView = createExceptionView(contentContainer);
            contentContainer.addView(exceptionView);
        }
    }

    protected View createExceptionView(ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.view_content_exception, parent, false);
    }

    public void showExceptionView(String status, String statusText) {
        showExceptionView(status, statusText, true);
    }

    public void showExceptionView(String status, String statusText, boolean showButton) {
        hideContentView();

        hideLoadingView();

        if (exceptionView == null) {
            initExceptionView();
        }

        ExceptionViewHolder holder = new ExceptionViewHolder(exceptionView);
        if (showButton) {
            holder.rootView.setOnClickListener(null);
            holder.refresh.setOnClickListener(onClickListener);
            holder.refresh.setVisibility(View.VISIBLE);
        } else {
            holder.rootView.setOnClickListener(v -> onClickListener.onClick(holder.refresh));
            holder.refresh.setOnClickListener(null);
            holder.refresh.setVisibility(View.GONE);
        }
        holder.statusText.setText(statusText);

        exceptionView.setVisibility(View.VISIBLE);
    }

    public void removeExceptionView() {
        if (exceptionView != null) {
            contentContainer.removeView(exceptionView);
            exceptionView = null;
        }
    }

    public void hideExceptionView() {
        if (exceptionView != null) {
            exceptionView.setVisibility(View.GONE);
        }
    }

    public void showPageFaultView() {
        showExceptionView("error", "页面加载失败");
    }

    public void showNoDataView() {
        showExceptionView("null", "");
    }

    public void showNoNetworkView() {
        showExceptionView("network_error", "页面加载失败");
    }

    public void hideContentView() {
        if (contentView != null)
            contentView.setVisibility(View.GONE);
    }

    public void showContentView() {
        if (contentView != null) {
            contentView.setVisibility(View.VISIBLE);
        }
    }

    public void completeLoading() {
        removeLoadingView();
        removeExceptionView();
        showContentView();
    }

    static class ExceptionViewHolder {

        final View rootView;
        ImageView image;
        TextView statusText;
        Button refresh;

        ExceptionViewHolder(View rootView) {
            this.rootView = rootView;

            this.image = rootView.findViewById(R.id.ivContentExceptionImage);
            this.statusText = rootView.findViewById(R.id.tvContentExceptionText);
            this.refresh = rootView.findViewById(R.id.btnContentExceptionRefresh);
        }
    }


}
